import 'package:flutter/material.dart';
import 'package:flutter_screenutil/flutter_screenutil.dart';
import 'package:get/get.dart';
import 'package:inmotion_pro/l10n/local.dart';
import 'package:inmotion_pro/pages/mine/vehilce_list/vehilce_auth_user_list/auth_user_item_view.dart';
import 'package:inmotion_pro/pages/models/user_model.dart';
import 'package:inmotion_pro/pages/others/common_enum/vehicle_owner_type.dart';
import 'package:lx_widgets_library/lx_widgets_library.dart';
import 'controller.dart';

// 车辆授权用户列表页面
class VehicleAuthUserListView
    extends BaseGetView<VehicleAuthUserListController> {
  VehicleAuthUserListView({super.key});

  @override
  String? get title => local.owner_info;

  @override
  Widget buildBody(BuildContext context) {
    return Stack(
      children: [
        Padding(
          padding: const EdgeInsets.only(bottom: 0),
          child: Obx(() {
            return Container(
              color: Colors.black,
              height: double.infinity,
              child: EmptyOrLoading(
                emptyMessage: local.no_data_available,
                isLoading: controller.state.authUserList.value == null,
                isEmpty: (controller.state.authUserList.value != null &&
                    controller.state.authUserList.value!.isEmpty),
                child: ListView.builder(
                  addAutomaticKeepAlives: false,
                  physics: const AlwaysScrollableScrollPhysics(),
                  itemCount: controller.state.authUserList.value?.length ?? 0,
                  itemBuilder: (context, index) {
                    UserModel item =
                        controller.state.authUserList.value![index];
                    return SizedBox(
                      height: 120.w,
                      child: Padding(
                        padding: EdgeInsets.only(
                          top: CONTENT_MARGIN / 2,
                          bottom: CONTENT_MARGIN / 2,
                          left: CONTENT_MARGIN,
                          right: CONTENT_MARGIN,
                        ),
                        child: AuthUserItemView(
                          model: item,
                          deleteOnTap: (p0) {
                            controller.disbandSecondOwnerOnTap(p0);
                          },
                          showDelete:
                              controller.myAuthorizedUserModel.ownerType ==
                                  VehicleOwnerType.owner, // 仅主车主可删除子项
                        ),
                      ),
                    );
                  },
                ),
              ),
            );
          }),
        ),
        Obx(
          () => Visibility(
            visible: controller.state.authUserList.value != null &&
                controller.state.authUserList.value!.isNotEmpty,
            child: Positioned(
                bottom: tabBarSpace + 2 * CONTENT_MARGIN,
                left: CONTENT_MARGIN * 2,
                right: CONTENT_MARGIN * 2,
                child: Column(
                  children: [
                    Visibility(
                      visible: controller.myAuthorizedUserModel.ownerType ==
                          VehicleOwnerType.coOwner,
                      child: Padding(
                        padding: EdgeInsets.only(
                            bottom: CONTENT_MARGIN,
                            left: CONTENT_MARGIN,
                            right: CONTENT_MARGIN),
                        child: LxText(
                          "* ${local.unbind_secondary_owner_tip}",
                          fontWeightEnum: LxFontWeightEnum.bodySmall,
                          color: Colors.red.withOpacity(0.8),
                          textAlign: TextAlign.left,
                        ),
                      ),
                    ),
                    LxButton(
                      height: 44.w,
                      borderRadius: BorderRadius.circular(20.w),
                      title: controller.myAuthorizedUserModel.ownerType ==
                              VehicleOwnerType.owner
                          ? context.s.unbind_owner
                          : context.s.unbind_secondary_owner,
                      buttonAlignmentEnum: ButtonAlignmentEnum.block,
                      buttonTypeCustomColor: ButtonTypeColor()
                        ..backgroundColor = GrayColor.gray2.withOpacity(0.5)
                        ..borderColor = Colors.transparent
                        ..textColor = Colors.white.withOpacity(0.8),
                      onTap: () {
                        switch (controller.myAuthorizedUserModel.ownerType) {
                          case VehicleOwnerType.owner:
                            controller.disbandOwnerOnTap();
                            break;
                          case VehicleOwnerType.coOwner:
                            controller.disbandSecondOwnerOnTap(
                                controller.myAuthorizedUserModel);
                            break;
                          default:
                            break;
                        }
                      },
                    ),
                  ],
                )),
          ),
        ),
      ],
    );
  }
}
